package com.dnjn.happiness.survey.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.amqp.AmqpSender;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.HappyConstants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.HxBeanSeed;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import com.dnjn.happiness.common.core.score.vo.HappySurvey;
import com.dnjn.happiness.common.core.score.vo.HappyVO;
import com.dnjn.happiness.survey.domain.Answer;
import com.dnjn.happiness.survey.domain.Survey;
import com.dnjn.happiness.survey.domain.SurveyIntervalScore;
import com.dnjn.happiness.survey.domain.SurveyRecord;
import com.dnjn.happiness.survey.domain.vo.AnswerVO;
import com.dnjn.happiness.survey.domain.vo.SurveyUserVO;
import com.dnjn.happiness.survey.mapper.AnswerMapper;
import com.dnjn.happiness.survey.mapper.SurveyRecordMapper;
import com.dnjn.happiness.survey.service.IAnswerService;
import com.dnjn.happiness.survey.service.IQuestionService;
import com.dnjn.happiness.survey.service.ISurveyIntervalScoreService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-10-04
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    IQuestionService questionService;

    @Autowired
    AnswerMapper answerMapper;

    @Autowired
    SurveyRecordMapper recordMapper;

    @Autowired
    private RemoteEmoUserService hxBeanService;

    @Autowired
    private ISurveyIntervalScoreService intervalScoreService;

    

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void goToAnswer(List<Answer> list) {
        list.stream().forEach(answer -> {
            Assert.notNull(answer.getUserId(), "答题者ID不能为空");
//            Assert.notNull(answer.getAnswerRight(), "AnswerRight不能为空");
            Assert.notNull(answer.getAnswer(), "您的答案不能为空");
            Assert.notNull(answer.getQuestionId(), "问题id不能为空");
            Assert.notNull(answer.getSurveyId(), "问卷id不能为空");
            answerMapper.insert(answer);
        });
        Answer answer = null;
        Integer surveyScore = null;
        String answerType = null;
        Integer grade = null;
        if (null != list && list.size() > 0) {
            answer = list.get(0);
            Long userId = answer.getUserId();
            Long surveyId = answer.getSurveyId();
            //计算当前答题的分值
            surveyScore = answer.getSurveyScore();
            answerType = answer.getAnswerType();
            //计算所属区间
            grade = null;
            List<SurveyIntervalScore> intervalScoreList = intervalScoreService.lambdaQuery().eq(SurveyIntervalScore::getSurveyId, answer.getSurveyId()).list();
            if (null != intervalScoreList && intervalScoreList.size() > 0 && !ObjectUtils.isEmpty(surveyScore)) {
                for (SurveyIntervalScore intervalScore : intervalScoreList) {
                    // 20 <= 27 <= 27
                    if (intervalScore.getStartScore() <= surveyScore && surveyScore <= intervalScore.getEndScore()) {
                        grade = intervalScore.getGrade();
                    }
                }
            }
            recordMapper.updateByUserId(userId, surveyId, surveyScore, grade, answerType);
        }
        // 完成问卷新增快乐豆
        if (!CollectionUtils.isEmpty(list) && list.size() > 0) {
            Long userId = list.get(0).getUserId();
            HxBeanSeed beanSeed = new HxBeanSeed();
            beanSeed.setUserId(userId);
            beanSeed.setRiskType(Constants.SURVEY_HLBEAN_CODE);
            R<Boolean> booleanR = hxBeanService.updateHBean(beanSeed, SecurityConstants.INNER);
        }

        // 影响快乐值-问卷

        if (null != list && list.size() > 0) {

            HappyVO vo = new HappyVO();
            HappySurvey happySurvey = new HappySurvey();


            // todo 计算当前答题的分值
            happySurvey.setGrade(grade);
            vo.setSurvey(happySurvey);
            vo.setUserId(answer.getUserId());
            vo.setRtype(HappyConstants.EMO_USER_RTYPE_SURVEY);
            AmqpSender.sendArgs(vo);

        }


    }


    @Override
    public Page<SurveyRecord> selectRecordList1(Long surveyId, Page page) {
        return recordMapper.selectRecordList1(surveyId, page);
    }

    @Override
    public Page<SurveyRecord> selectRecordList(Survey domain, Page page) {
        return recordMapper.selectRecordList(domain, page);
    }

    @Override
    public Page<Answer> selectAnswerList(SurveyRecord domain, Page page) {
        return answerMapper.selectAnswerList(domain, page);

    }

    @Override
    public List<Answer> selectAnswerCount(String option, Long questionId) {
        return baseMapper.selectAnswerCount(option, questionId);
    }

    @Override
    public List<Answer> selectAnswerListAndScore(Long questionId) {
        return baseMapper.selectAnswerListAndScore(questionId);
    }

    @Override
    public List<SurveyUserVO> screeningSubmit(AnswerVO answerVO) {

        return baseMapper.screeningSubmit(answerVO);
    }


}
